Load imager
plant<- load.image('~/Documents/Github/PhD//imageprocessing/burseragrav1.jpg')
dim(plant)
## [1] 1400 2100 1 3
plot(plant)
## Y-axis shift
imshift(plant,0,50) %>% plot
## X-Axis
imshift(plant,50,0) %>% plot
imshift(plant,50,50) %>% plot
map_il(2:4,~ imresize(plant,1/.)) %>% imappend("x") %>% plot
imrotate(plant,30) %>% plot
grayscale(plant) %>% plot
msk <- px.flood(plant,400,900,sigma=.28) %>% as.cimg
inv<- msk %>%as.data.frame
inv$value<- as.numeric(inv$value==0)
msk<- inv %>% as.cimg()
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
plot(msk*plant)
subsetImage<- function(test,xrange,yrange){
newtest<- test %>% as.data.frame
dfnew<-subset(newtest, x%in%xrange & y%in%yrange)
dfnew<- as.cimg(dfnew)
return(dfnew)}
indicatorMatrix <-function( test,xrange,yrange,types)
{
if(types=='RBG'){
newtest<- test %>% as.data.frame} else{newtest<-RGBtoHSV(test) %>% as.data.frame}
dfnew<-subset(newtest, x%in%xrange & y%in%yrange) %>% ddply( .(cc),summarize,SD = sd(value, na.rm=TRUE),Mean = mean(value, na.rm=TRUE))
tmp<-subset(newtest,cc==1)
tmp$value<-as.numeric( between (newtest$value[newtest$cc==1] , unlist(dfnew[1,3]-dfnew[1,2]), unlist(dfnew[1,3]+dfnew[1,2]) ) & between ( newtest$value[newtest$cc==2 ] , unlist(dfnew[2,3]-dfnew[2,2]), unlist(dfnew[2,3]+dfnew[2,2]))
& between ( newtest$value[newtest$cc==3] , unlist(dfnew[3,3]-dfnew[3,2]), unlist(dfnew[3,3]+dfnew[3,2])))
newtest$value<- rep(tmp$value,3) *newtest$value
tester<- newtest %>% as.cimg
return(list(tester,tmp,dfnew))
}
samplemat3<- indicatorMatrix(plant,900:1000 ,850:950,'RBG')
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
#Isolate Leaves
plot(samplemat3[[1]])
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
samplemathsv<- indicatorMatrix(plant,900:1000 ,850:950,'HSV')
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
#Isolate Leaves
plot(samplemathsv[[1]])
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
gray<- grayscale(samplemat3[[1]] )
grayshrink<- shrink(gray,8)
#Isolate Leaves
plot(grayshrink)
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
gray<- grayscale(samplemat3[[1]] )
graygrow<- grow(gray,2)
#Isolate Leaves
plot(graygrow)
#Correlate2D<-function ( A, B ){
# a = ft.fft2(A)
## c = a %*% b
# C = ft.ifft2( c )
# C = ft.fftshift(C)
# return C
#}
#bw <- plant %>% grayscale
#ff <- FFT(bw)
#a=ff %>% as.data.frame
#plot(ff$real,main="Real part of the transform")
gray<- grayscale(plant )
pcamat<- gray %>% as.data.frame %>% data.table
pcamat2<- data.table::dcast(pcamat,x~y, value.var='value') %>% as.matrix
photo.pca <- prcomp(pcamat, center = F)
#PCs <- c(round(0.9 * nrow(pcamat2)))
photo<- pcamat
photo[,3]<- pcamat[,3]*photo.pca$x[3,1]
photo%>% as.cimg%>% plot
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
#Isolate Leaves
Area<- sum(samplemat3[[2]]$value)
print(Area)
## [1] 102595
binImageShrink <- samplemat3[[2]] %>% as.cimg %>% shrink(1) %>% as.data.frame
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
Perimeter<- sum(samplemat3[[2]]$value)- sum(binImageShrink$value)# ) %>% as.cimg %>%
print(Perimeter)
## [1] 102595
binImageShrink <- samplemat3[[2]] %>% as.cimg %>% shrink(1) %>% as.data.frame
## Warning in as.cimg.data.frame(.): Guessing image dimensions from maximum
## coordinate values
sum(samplemat3[[2]]$value)- sum(binImageShrink$cc)# ) %>% as.cimg %>% plot
## [1] 0
#Isolate Leaves
subsetDF<- subset (samplemat3[[2]], value==1)
mean(subsetDF$x)
## [1] 776.762
mean(subsetDF$y)
## [1] 872.7603
#Isolate Leaves
#Use cannyedges
leafimage<-subim(plant, x%inr% c(450,600),y%inr% c(1175,1500))
edges<-cannyEdges(leafimage)
## Warning in cannyEdges(leafimage): Running Canny detector on luminance
## channel
plot(edges)
#Isolate Leaves
#Use cannyedges
library('EBImage')
##
## Attaching package: 'EBImage'
## The following object is masked from 'package:data.table':
##
## transpose
## The following objects are masked from 'package:imager':
##
## channel, dilate, display, erode, resize, watershed
img<-readImage('~/Documents/Github/PhD//imageprocessing/burseragrav1.jpg')
grayimage<-channel(img,"gray")
crop<-grayimage[450:600,1175:1500]
y = gblur(crop, 3) < .8
y <- bwlabel(y)
contours = ocontour(bwlabel(y))
c = localCurvature(x=contours[[1]], h=11)
i = c$curvature >= 0
pos = neg = array(0, dim(crop))
pos[c$contour[i,]+1] = c$curvature[i]
neg[c$contour[!i,]+1] = -c$curvature[!i]
display(10*(rgbImage(pos, neg)), title = "Image curvature")
#Isolate Leaves